$(document).ready(function(){
	$("#btnFilter").click(function(){
		filterBySelectedOption();
	});
	$("#bthAdd").click(function(){
		popupAddDialog();
	});
	filterBySelectedOption();
});

function filterBySelectedOption() {
	filterUser($("#selectActive").val());
}

function filterUser(active) {
	$("#txtMessage").text("页面正在加载...")
	var url = location.href + "/..";
    $.ajax({
        type: "GET",
        url: url,
        data: {active: active},
        dataType: "json",
        success: function (resp) {
            if (resp.code == "0") {
                var tableBodyObj = $("#tableUsers tbody");
                tableBodyObj.html("");
                $.each(resp.data,function(index, item) {
                	var rowHtml = "<tr>";
                	rowHtml += "<td>" + item.id + "</td>";
                	rowHtml += "<td>" + item.loginName + "</td>";
                	rowHtml += "<td>" + item.showName + "</td>";
                	rowHtml += "<td>" + item.active + "</td>";
                	rowHtml += "<td>";
                	rowHtml += "<span class='clickable' onclick='popupEditDialog(event)'>修改</span>";
                	rowHtml += "<span class='clickable' onclick='popupPasswdDialog(event)'>设置密码</span>";
                	rowHtml += "</td>";
                	rowHtml += "</tr>";
                	tableBodyObj.append(rowHtml);
                });
                
                var countSummaryObj = $("#txtCountSummary");
                countSummaryObj.html("共 " + resp.data.length + " 条记录");
                
            } else {
                alert("服务器返回出错信息：\n\n" + resp.message);
            }
        	$("#txtMessage").text("");
        },
        error: function () { alert("Sorry，服务器处理请求出错，请联系IT运维人员。"); }
    });
}

var dialogAdd = $("#dialogAdd").dialog({
    autoOpen: false,
    height:220,
    width: 300,
    modal: true,
    buttons: {
    	"创建用户": addUser,
    	Cancel: function() {
    		dialogAdd.dialog("close");
    	}
	},
	close: function() {
    	$("#dialogAdd #loginName").val("");
    	$("#dialogAdd #showNameName").val("");
    	$("#dialogAdd #password").val("");
	}
});

function popupAddDialog() {
	dialogAdd.dialog("open");
}

function addUser() {
	var loginName = $("#dialogAdd #loginName").val();
	var showName = $("#dialogAdd #showName").val();
	var password = $("#dialogAdd #password").val();
	var postData = {loginName:loginName, showName:showName, password:password};
	var url = location.href + "/..";
	debugger
    $.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify(postData),
        dataType: "json",
        contentType:"application/json",
        success: function (resp) {
            if (resp.code == "0") {
            	dialogAdd.dialog("close");
            	filterUser(); // 刷新页面
                
            } else {
            	alert("服务器返回出错信息：\n\n" + resp.message);
            }
        },
        error: function() { alert("Sorry，服务器处理请求出错，请联系IT运维人员。"); }
    });    
}

var dialogEdit = $("#dialogEdit").dialog({
    autoOpen: false,
    height:200,
    width: 300,
    modal: true,
    buttons: {
    	"保存修改内容": updateUser,
    	Cancel: function() {
    		dialogEdit.dialog("close");
    	}
	}
});

function popupEditDialog(event) {
	event = event || window.event;
    var sourceObj = event.srcElement || event.target;
    var fields = $(sourceObj).closest("td").siblings();
    var active = fields[3].innerText;
    $("#dialogEdit #id").val(fields[0].innerText);
    $("#dialogEdit #loginName").text(fields[1].innerText);
    $("#dialogEdit #showName").val(fields[2].innerText);
    $("#dialogEdit #active").val(fields[3].innerText);
    dialogEdit.dialog("open");
}

function updateUser() {
	var id = $("#dialogEdit #id").val();
	var showName = $("#dialogEdit #showName").val();
	var active = $("#dialogEdit #active").val();
	var postData = {showName:showName, active:active};

    $.ajax({
        type: "POST",
        url: id,
        data: JSON.stringify(postData),
        dataType: "json",
        contentType:"application/json",
        success: function (resp) {
            if (resp.code == "0") {
            	dialogEdit.dialog("close");
            	filterUser(); // 刷新页面               
            } else {
            	alert("服务器返回出错信息：\n\n" + resp.message);
            }
        },
        error: function () { alert("Sorry，服务器处理请求出错，请联系IT运维人员。"); }
    });
}

var dialogPasswd = $("#dialogPasswd").dialog({
    autoOpen: false,
    height:220,
    width: 300,
    modal: true,
    buttons: {
    	"保存新密码": updatePassword,
    	Cancel: function() {
    		dialogPasswd.dialog("close");
    	}
	}
});

function popupPasswdDialog(event) {
	event = event || window.event;
    var sourceObj = event.srcElement || event.target;
    var fields = $(sourceObj).closest("td").siblings();
    var active = fields[3].innerText;
    $("#dialogPasswd #id").val(fields[0].innerText);
    $("#dialogPasswd #loginName").text(fields[1].innerText);
    $("#dialogPasswd #showName").text(fields[2].innerText);
    $("#dialogPasswd #password").val();
    dialogPasswd.dialog("open");
}
 
function updatePassword() {
	var id = $("#dialogPasswd #id").val();
	var password = $("#dialogPasswd #password").val();
	var postData = {password:password};
	var url = getParentPath(location.pathname) + id + "/passwd";
    $.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify(postData),
        dataType: "json",
        contentType:"application/json",
        success: function (resp) {
            if (resp.code == "0") {
            	dialogPasswd.dialog("close");
            	filterUser(); // 刷新页面               
            } else {
            	alert("服务器返回出错信息：\n\n" + resp.message);
            }
        },
        error: function () { alert("Sorry，服务器处理请求出错，请联系IT运维人员。"); }
    });
}

function getParentPath(url) {
	var arr = url.split("/");
	var strPage = arr[arr.length-1];
	var index = url.indexOf(strPage);
	var parent = url.substr(0, index); 
	return parent;
}
